package org.andreidario;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class BioklaimAnalizer {
	
	static void genList() {
		int numberOfLinesPerFile = 10000;
		int row = 10;
		
		String[] vals = new String[numberOfLinesPerFile];
		int files = 0;
		BufferedWriter bw;
		BufferedReader br;
		
		for (int k = 0; k < vals.length; ++k) {
			vals[k] = "";
		}
		
		try {
			for (int i = 0; i < row; ++i){
				br = new BufferedReader(new FileReader(new File("Input_Files/List/Ddp_Cell_"+i+"_0.dat")));
				files = 0;
				String ret = br.readLine();
				ret = br.readLine();
				while (ret != null && files < numberOfLinesPerFile) {
					vals[files] += i+" 0 "+ret.split(" ")[1]+"\r\n"+i+" 1 "+ret.split(" ")[1]+"\r\n\r\n";
					
					ret = br.readLine();
					files++;
				}
				br.close();
			}
			
			files = 0;
			
			while (files < numberOfLinesPerFile) {
				bw = new BufferedWriter(new FileWriter(new File("Output_Files/List/Ddp"+files+".dat")));
				bw.write(vals[files]);
				bw.close();
				files++;
				System.out.println(files);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	static void genSquare(String path) {
		
		int numberOfLinesPerFile = 10000;
		int row = 10;
		int columns = 10;
		
		String[] vals = new String[numberOfLinesPerFile];
		int files = 0;
		BufferedWriter bw;
		BufferedReader br;
		
		for (int k = 0; k < vals.length; ++k) {
			vals[k] = "";
		}
		
		try {
			for (int i = 0; i < row; ++i){
				for (int j = 0; j < columns; ++j) {
					br = new BufferedReader(new FileReader(new File("Input_Files/"+path+"/Ddp_Cell_"+i+"_"+j+".dat")));
					files = 0;
					System.out.print("Lettura file Ddp_Cell_"+i+"_"+j+".dat ...");
					String ret = br.readLine();
					ret = br.readLine();
					while (ret != null && files < numberOfLinesPerFile) {
						vals[files] += i+" "+j+" "+ret.split(" ")[1]+"\r\n";
						
						if (j == (columns - 1)) {
							vals[files] += "\r\n";
						}
						ret = br.readLine();
						files++;
					}
					br.close();
					System.out.println("Fatto.");
				}
			}
			
			files = 0;
			
			while (files < numberOfLinesPerFile) {
				bw = new BufferedWriter(new FileWriter(new File("Output_Files/"+path+"/Ddp"+files+".dat")));
				bw.write(vals[files]);
				bw.close();
				files++;
				System.out.println(files);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		//genList();
		genSquare("Square-Central");
	}
}
